//
//  MKAPITaskProgressTestCase.m
//  MKAPITests
//
//  Created by 苏尚进 on 2022/8/15.
//  Copyright © 2022 moon. All rights reserved.
//

#import "MKAPITaskProgressTestCase.h"
#import "NSTimer+MKAPITest.h"
#import "MKAPITestTimerAPIBase.h"
#import <MKAPI/MKAPI.h>

@interface __MKAPITaskProgressTestCase_APITest : MKAPITestTimerAPIBase

@end

@implementation __MKAPITaskProgressTestCase_APITest
@end


@implementation MKAPITaskProgressTestCase
- (void)test_progresschild{
    NSProgress *rootProgress = [NSProgress progressWithTotalUnitCount:100];
    rootProgress.completedUnitCount += 10;
    rootProgress.completedUnitCount += 10;
    NSProgress *progress1 = [NSProgress progressWithTotalUnitCount:10000];
    NSProgress *progress2 = [NSProgress progressWithTotalUnitCount:10000];
    NSProgress *joinPorgress = [NSProgress progressWithTotalUnitCount:10000];
    [joinPorgress addChild:progress1 withPendingUnitCount:joinPorgress.totalUnitCount/2];
    [joinPorgress addChild:progress2 withPendingUnitCount:joinPorgress.totalUnitCount/2];
    [rootProgress addChild:joinPorgress withPendingUnitCount:rootProgress.totalUnitCount-rootProgress.completedUnitCount];
    NSLog(@"rootProgress:%@",rootProgress);
    
    progress1.completedUnitCount = progress1.totalUnitCount;
    [progress1 mkapi_setCompletedUnitCount:progress1.totalUnitCount*2 totalUnitCount:progress1.totalUnitCount*2];
    NSLog(@"rootProgress:%@",rootProgress);
    
    progress2.completedUnitCount = progress2.totalUnitCount;
    [progress2 mkapi_setCompletedUnitCount:progress2.totalUnitCount*0.5 totalUnitCount:progress2.totalUnitCount*0.5];
    NSLog(@"rootProgress:%@",rootProgress);
    XCTAssertTrue(rootProgress.fractionCompleted==1,@"进度依赖错误");
}
- (void)test_signleApiProgress{//进度测试
    XCTestExpectation *e0 = [self expectationWithDescription:@"test_signleApiProgress_0"];
    XCTestExpectation *e1 = [self expectationWithDescription:@"test_signleApiProgress_0.5"];
    __block BOOL e0_fulfill = NO;
    __block BOOL e1_fulfill = NO;
    __block BOOL e2_fulfill = NO;
    XCTestExpectation *e2 = [self expectationWithDescription:@"test_signleApiProgress_1"];
    __MKAPITaskProgressTestCase_APITest *api = [[__MKAPITaskProgressTestCase_APITest alloc] init];
    [api addProgressHandler:^(__kindof MKAPITask * _Nonnull task, NSProgress * _Nonnull progress) {
        CGFloat progressPercent = progress.mkapi_progressPercent;
        NSLog(@"progress:%0.2f,isFinished:%@",progressPercent,@(task.progress.isFinished));
        if(progressPercent==0){
            if(!e0_fulfill){
                e0_fulfill = YES;
                [e0 fulfill];
            }
        }
        if(progressPercent>=0.5){
            if(!e1_fulfill){
                e1_fulfill = YES;
                [e1 fulfill];
            }
        }
        if(progress.isFinished){
            if(!e2_fulfill){
                e2_fulfill = YES;
                [e2 fulfill];
            }
        }
    }];
    [api run];
    [self waitForExpectations:@[e0,e1,e2] timeout:5];
}
- (void)test_signleApiProgressMultipHandler{//进度测试
    XCTestExpectation *e0 = [self expectationWithDescription:@"test_signleApiProgress_0"];
    XCTestExpectation *e1 = [self expectationWithDescription:@"test_signleApiProgress_0.5"];
    __block BOOL e0_fulfill = NO;
    __block BOOL e1_fulfill = NO;
    __block BOOL e2_fulfill = NO;
    XCTestExpectation *e2 = [self expectationWithDescription:@"test_signleApiProgress_1"];
    __MKAPITaskProgressTestCase_APITest *api = [[__MKAPITaskProgressTestCase_APITest alloc] init];
    [api addProgressHandler:^(__kindof MKAPITask * _Nonnull task, NSProgress * _Nonnull progress) {
        CGFloat progressPercent = progress.mkapi_progressPercent;
        NSLog(@"progress:%0.2f,isFinished:%@",progressPercent,@(task.progress.isFinished));
        if(progressPercent==0){
            if(!e0_fulfill){
                e0_fulfill = YES;
                [e0 fulfill];
            }
        }
    }];
    [api addProgressHandler:^(__kindof MKAPITask * _Nonnull task, NSProgress * _Nonnull progress) {
        CGFloat progressPercent = task.progress.mkapi_progressPercent;
        NSLog(@"progress:%0.2f,isFinished:%@",progressPercent,@(task.progress.isFinished));
        if(progressPercent>=0.5){
            if(!e1_fulfill){
                e1_fulfill = YES;
                [e1 fulfill];
            }
        }
    }];
    [api addProgressHandler:^(__kindof MKAPITask * _Nonnull task, NSProgress * _Nonnull progress) {
        CGFloat progressPercent = task.progress.mkapi_progressPercent;
        NSLog(@"progress:%0.2f,isFinished:%@",progressPercent,@(task.progress.isFinished));
        if(task.progress.isFinished){
            if(!e2_fulfill){
                e2_fulfill = YES;
                [e2 fulfill];
            }
        }
    }];
    [api run];
    [self waitForExpectations:@[e0,e1,e2] timeout:5];
}
- (void)test_signleApiProgressTmpAddHandler{//进度测试
    XCTestExpectation *e0 = [self expectationWithDescription:@"test_signleApiProgress_0"];
    XCTestExpectation *e1 = [self expectationWithDescription:@"test_signleApiProgress_0.5"];
    __block BOOL e0_fulfill = NO;
    __block BOOL e1_fulfill = NO;
    __block BOOL e2_fulfill = NO;
    
    __block BOOL e1_addHandler = NO;
    XCTestExpectation *e2 = [self expectationWithDescription:@"test_signleApiProgress_1"];
    
    __MKAPITaskProgressTestCase_APITest *api = [[__MKAPITaskProgressTestCase_APITest alloc] init];
    [api addProgressHandler:^(__kindof MKAPITask * _Nonnull task, NSProgress * _Nonnull progress) {
        CGFloat progressPercent = task.progress.mkapi_progressPercent;
        NSLog(@"progress:%0.2f,isFinished:%@",progressPercent,@(task.progress.isFinished));
        if(progressPercent==0){
            if(!e0_fulfill){
                e0_fulfill = YES;
                [e0 fulfill];
            }
        }
        if(progressPercent>=0.1){
            if(!e1_addHandler){
                e1_addHandler = YES;
                [task addProgressHandler:^(__kindof MKAPITask * _Nonnull task, NSProgress * _Nonnull progress) {
                    CGFloat progressPercent = task.progress.mkapi_progressPercent;
                    NSLog(@"progress:%0.2f,isFinished:%@",progressPercent,@(task.progress.isFinished));
                    if(progressPercent>=0.5){
                        if(!e1_fulfill){
                            e1_fulfill = YES;
                            [e1 fulfill];
                        }
                    }
                }];
                
                [task addProgressHandler:^(__kindof MKAPITask * _Nonnull task, NSProgress * _Nonnull progress) {
                    CGFloat progressPercent = task.progress.mkapi_progressPercent;
                    NSLog(@"progress:%0.2f,isFinished:%@",progressPercent,@(task.progress.isFinished));
                    if(task.progress.isFinished){
                        if(!e2_fulfill){
                            e2_fulfill = YES;
                            [e2 fulfill];
                        }
                    }
                }];
            }
        }
    }];
    [api addProgressHandler:^(__kindof MKAPITask * _Nonnull task, NSProgress * _Nonnull progress) {
        
    }];
    [api run];
    [self waitForExpectations:@[e0,e1,e2] timeout:5];
}
- (void)test_signleApiProgressCancelTest{
    XCTestExpectation *e0 = [self expectationWithDescription:@"test_signleApiProgress_0"];
    XCTestExpectation *e1 = [self expectationWithDescription:@"test_signleApiProgress_0.5"];
    __block BOOL e0_fulfill = NO;
    XCTestExpectation *e2 = [self expectationWithDescription:@"test_signleApiProgress_1"];
    __MKAPITaskProgressTestCase_APITest *api = [[__MKAPITaskProgressTestCase_APITest alloc] init];
    [api addProgressHandler:^(__kindof MKAPITask * _Nonnull task, NSProgress * _Nonnull progress) {
        CGFloat progressPercent = task.progress.mkapi_progressPercent;
        NSLog(@"progress:%0.2f,isFinished:%@",progressPercent,@(task.progress.isFinished));
        if(progressPercent==0){
            if(!e0_fulfill){
                e0_fulfill = YES;
                [e0 fulfill];
            }
        }
    }];
    dispatch_queue_t queue = dispatch_queue_create([[NSString stringWithFormat:@"testApi_%@",self.name] UTF8String], nil);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), queue, ^{
        [[MKAPITaskSession sharedSession] runCodeInQueue:^{
            [api cancel];
            CGFloat percent = api.progress.mkapi_progressPercent;
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                CGFloat percent2 = api.progress.mkapi_progressPercent;
                if(percent==percent2){
                    [e1 fulfill];
                }
            });
        }];
    });
    [api addCompletionHandler:^(__kindof MKAPITask * _Nonnull task, BOOL success, NSError * _Nullable error) {
        if(error){
            [e2 fulfill];
        }
    }];
    [api run];
    [self waitForExpectations:@[e0,e1,e2] timeout:5];
}
@end
